﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiskInventory
{
    public partial class DiskInventoryDataSet
    {
        /// <summary>
        /// The name of the XML file (no path)
        /// </summary>
        public const string DefaultName = "DiskInventory.xml";
        /// <summary>
        /// The XML file path with the file name
        /// </summary>
        public static string DefaultPath()
        {
            return GetAppData(DefaultName);
        }
        /// <summary>
        /// The user's application directory, created if it does not exists
        /// </summary>
        /// <param name="aSub">Package name</param>
        /// <returns>Application directory</returns>
        public static string GetApplicationDirectory(string aSub)
        {
            string Result = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), aSub);
            if (!System.IO.Directory.Exists(Result)) System.IO.Directory.CreateDirectory(Result);
            return Result;
        }
        /// <summary>
        /// The application path and filename, making sure it exists
        /// </summary>
        public static string GetAppData(string aFileName)
        {
            string Result = GetApplicationDirectory(@"NOAANMFS");
            if (!System.IO.Directory.Exists(Result)) System.IO.Directory.CreateDirectory(Result);
            return System.IO.Path.Combine(Result, aFileName);
        }
        /// <summary>
        /// Load the database from the deefault XML file
        /// </summary>
        /// <returns>null if OK, else the exception</returns>
        public Exception Load()
        {
            return Load(string.Empty);
        }
        /// <summary>
        /// Load the database from a file (if empty, use the default)
        /// </summary>
        /// <param name="aFullPath">File to load or default if null or empty</param>
        /// <returns>null if OK, else the exception</returns>
        public Exception Load(string aFullPath)
        {
            if (string.IsNullOrEmpty(aFullPath)) aFullPath = DefaultPath();
            if (!System.IO.File.Exists(aFullPath)) return new Exception("File not found: " + aFullPath);
            this.Clear();
            Exception Result = null;
            try
            {
                this.ReadXml(aFullPath);
            }
            catch (Exception Ex)
            {
                Result = Ex;
            }
            return Result;
        }
        /// <summary>
        /// Saves the dataset to the default XML
        /// </summary>
        /// <returns>null if OK, else the exception</returns>
        public void Save()
        {
            Save(GetAppData(DefaultName));
        }
        /// <summary>
        /// Saves the dataset to the passed file
        /// </summary>
        /// <param name="aFullPath">XML file to load</param>
        /// <returns>null if OK, else the exception</returns>
        public void Save(string aFullPath)
        {
            if (aFullPath.EndsWith(".csv", StringComparison.InvariantCultureIgnoreCase))
                WriteCSV(aFullPath);
            else
                WriteXml(aFullPath);
        }
        /// <summary>
        /// Output the dataset as CSV
        /// </summary>
        /// <param name="aFullPath">File to save</param>
        public void WriteCSV(string aFullPath)
        {
            List<string> Lines = new List<string>()
            {
                string.Join(",",this.tableDiskInventory.Columns.Cast<System.Data.DataColumn>().Select(qN=>qN.ColumnName).ToArray()),
            };
            Lines.AddRange(
                this.DiskInventory.Select(qR=>string.Join(",",
                    (qR.ItemArray).Select(qS=>"\""+qS.ToString()
                            .Replace("\"", "\"\"")
                            +"\"")
                    )).ToArray());
            System.IO.File.WriteAllLines(aFullPath, Lines.ToArray());
        }
    }
}
